package com.mapbased.sfw.component;

import org.dom4j.Element;

import com.mapbased.sfw.model.Fragment;
import com.mapbased.sfw.model.FragmentBag;
import com.mapbased.sfw.model.LoadContext;
import com.mapbased.sfw.model.RenderContext;
import com.mapbased.sfw.model.Renderable;

public class Import implements Renderable, Xmlable, Headable {

	private FragmentBag defaultPagebag;
	private String fragmentPath;

	@Override
	public void render(RenderContext rc) {
		if (defaultPagebag != null) {
			Fragment f = defaultPagebag.getFragment();
			if (f != null) {
				rc.setCurrentFragment(f);
				f.render(rc);
			}
		}

	}

	@Override
	public String getTypeName() {
		return "Import";
	}

	@Override
	public void parseXml(Element ele, LoadContext lc) {
		fragmentPath = lc.fetchAttribute(ele, "path");
		this.defaultPagebag = lc.getSite().getFragmentBag(fragmentPath);

	}

	@Override
	public void toXml(XmlContext xc) {
		String s = this.fragmentPath;
		xc.sb.append("<div jid=\"import\" path=\"").append(s).append("\" >")
				.append("</div>");

	}

	@Override
	public void doHead(RenderContext rc) {
		if (defaultPagebag != null) {
			Fragment f = defaultPagebag.getFragment();
			if (f != null) {
				f.prepareHeadables(rc);
			}
		}

	}

}
